package com.richard.base.ui

import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import java.util.*

/**
 ***************************************
 * 项目名称:Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/7/26     12:38 PM
 * 用途:
 ***************************************
 */
abstract class CommonPagerAdapter<T> : PagerAdapter() {
    private val viewCache = SparseArray<View>()
    private val dataList: MutableList<T> = ArrayList()
    private var onItemClickListener: OnItemClickListener? = null
    private var onItemLongClickListener: OnItemLongClickListener? = null

    fun setDataList(list: List<T>) {
        dataList.clear()
        dataList.addAll(list)
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        this.onItemClickListener = onItemClickListener
    }

    fun setOnItemLongClickListener(onItemLongClickListener: OnItemLongClickListener?) {
        this.onItemLongClickListener = onItemLongClickListener
    }

    override fun getCount(): Int {
        return dataList.size
    }

    fun getRealCount(): Int {
        return dataList.size ?: 0
    }

    override fun isViewFromObject(
        view: View,
        item: Any
    ): Boolean {
        return view === item
    }

    override fun instantiateItem(container: ViewGroup, position: Int): View {
        val itemView = getItemView(container, position)
        if (onItemClickListener != null) {
            itemView.setOnClickListener { v -> onItemClickListener!!.onItemClick(v, position) }
        }
        if (onItemLongClickListener != null) {
            itemView.setOnLongClickListener { v ->
                onItemLongClickListener!!.onItemLongClick(
                    v,
                    position
                )
            }
        }
        itemView.tag = position
        return itemView
    }

    /**
     * 获取ViewPager itemView
     * 若缓存中存在itemView则取之，不存在新生成
     */
    private fun getItemView(container: ViewGroup, position: Int): View {
        var itemView = viewCache[position]
        if (itemView == null) {
            itemView = getPageItemView(container, position)
            viewCache.put(position, itemView)
        }
        renderItemView(itemView, position)
        container.addView(itemView)
        return itemView
    }

    /**
     * 渲染itemView
     */
    abstract fun renderItemView(itemView: View, position: Int)

    /**
     * 获取itemView
     * @see .getItemView
     */
    abstract fun getPageItemView(container: ViewGroup, position: Int): View

    override fun destroyItem(
        container: ViewGroup,
        position: Int,
        item: Any
    ) {
        val view = item as View
        container.removeView(view)
    }

    fun getBindItemData(position: Int): T? {
        return if (dataList != null && dataList.size > position && position >= 0) {
            dataList[position]
        } else null
    }

    interface OnItemClickListener {
        fun onItemClick(view: View?, position: Int)
    }

    interface OnItemLongClickListener {
        fun onItemLongClick(view: View?, position: Int): Boolean
    }
}